<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'site_settings';
	private $_ci;
	protected $validation_rules = array(
			'min_price_per_hour' => array(
				'field' => 'min_price_per_hour',
				'label' => 'lang:escort_site_setting_price_selection_min_label',
				'rules' => 'trim|required|max_length[100]|integer'
			),
			'max_price_per_hour' => array(
							'field' => 'max_price_per_hour',
							'label' => 'lang:escort_site_setting_price_selection_max_label',
							'rules' => 'trim|required|max_length[100]|integer'
			)
			
	);
	public function __construct()
	{
		parent::__construct();
		$this->load->model('escort_site_settings_m');
		$this->load->model('escort_site_settings_on_zones_m');
		$this->lang->load('site_setting');
		$this->load->library(array('form_validation'));
		$this->_ci = &get_instance();
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
						->append_metadata(js('escort.site.settings.js', 'site_setting'))
						->append_metadata(js('jquery.simplemodal.js', 'site_setting'))
						->append_metadata(css('escort.site.settings.css', 'site_setting'))
						->append_metadata(css('confirm.css', 'site_setting'));
	}

	/**
	 * List all items
	 */
	public function index()
	{
		$this->data->sites = array();
		$siteEntity = new CI_Site_entity();
		// Create pagination links
		$total_rows = $siteEntity->count();
		$pagination = create_pagination('admin/site_setting/index', $total_rows);

		$siteList = $siteEntity->getSiteList();
		//print_r($sites);
		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		
		$this->template
			->title($this->module_details['name'])
			->append_metadata(js('admin/filter.js'))
			->set('pagination', $pagination)
			->set('siteList', $siteList);
		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}
	public function create()
	{
		$this->form_validation->set_rules($this->validation_rules);
	
	
		$created_time = time();
	
	
		if ($this->form_validation->run())
		{
			$escortNewsEntity = new CI_Escort_news_entity();
			$escortNewsEntity->setTitle($this->input->post('title'));
			$escortNewsEntity->setLanguageId($this->input->post('language_id'));
			$escortNewsEntity->setCategoryId($this->input->post('category_id'));
			$escortNewsEntity->setNews($this->input->post('news'));
			$escortNewsEntity->setStatus($this->input->post('status'));
			$escortNewsEntity->setCreatedTime($created_time);
			$escortNewsEntity->setSiteRef($this->input->post('site_ref'));
			$id = $escortNewsEntity->save();
			if ($id)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('blog_post_add_success'), $this->input->post('title')));
			}
			else
			{
				$this->session->set_flashdata('error', $this->lang->line('blog_post_add_error'));
			}
			$this->input->post('btnAction') == 'save_exit' ? redirect('admin/news') : redirect('admin/news/edit/' . $id);
		}
		else
		{
			// Go through all the known fields and get the post values
			foreach ($this->validation_rules as $key => $field)
			{
				$post->$field['field'] = set_value($field['field']);
			}
			$post->created_time = $created_time;
		}
	
		$this->template
		->title($this->module_details['name'], lang('escort_news_create_title'))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
		->append_metadata(js('jquery/jquery.tagsinput.min.js'))
		->append_metadata(js('blog_form.js', 'blog'))
		->append_metadata(css('jquery/jquery.tagsinput.css'))
		->set('post', $post)
		->build('admin/form');
	}
	public function edit($id = 0)
	{
		$id OR redirect('admin/site_setting');
	
		$post = $this->escort_site_settings_m->get($id);
		$siteSettingZones = $this->escort_site_settings_on_zones_m->get_by_site($id);
		
		if(!$post){
			$id = $this->escort_site_settings_m->insert(array(
							'site_id'				=> $id,
							'main_site_language_ids' 	=> 0,
							'escort_category_code'		=> 0,
							'sexual_orientation_code'	=> 0,
							'escort_priority_id'	=> 0,
							'min_price_per_hour'	=> 0,
							'max_price_per_hour'	=> 0,
							'hair_color_code'	=> 0,
							'hair_size_code'	=> 0,
							'hair_style_code'	=> 0,
							'eye_code'	=> 0,
							'height'	=> 0,
							'weight'	=> 0,
							'measure_1'	=> 0,
							'measure_2'	=> 0,
							'measure_3'	=> 0
							));
			redirect('admin/site_setting/edit/' . $id);
			
		}
		$this->form_validation->set_rules($this->validation_rules);
	
		if ($this->form_validation->run())
		{
			$main_site_language_ids = implode(',', $this->input->post('language_id'));
			$result = $this->escort_site_settings_m->update($id, array(
						'site_id'				=> $id,
						'main_site_language_ids' 	=> $main_site_language_ids,
						'escort_category_code'		=> $this->input->post('escort_category_code'),
						'sexual_orientation_code'	=> $this->input->post('sexual_orientation_code'),
						'escort_priority_id'	=> $this->input->post('escort_priority_id'),
						'min_price_per_hour'	=> $this->input->post('min_price_per_hour'),
						'max_price_per_hour'	=> $this->input->post('max_price_per_hour'),
						'hair_color_code'	=> $this->input->post('hair_color_code'),
						'hair_size_code'	=> $this->input->post('hair_size_code'),
						'hair_style_code'	=> $this->input->post('hair_style_code'),
						'eye_code'	=> $this->input->post('eye_code'),
						'height'	=> $this->input->post('height'),
						'weight'	=> $this->input->post('weight'),
						'measure_1'	=> $this->input->post('measure_1'),
						'measure_2'	=> $this->input->post('measure_2'),
						'measure_3'	=> $this->input->post('measure_3')
			));
			
			$countryList = $this->input->post('country_id');
			$regionList = $this->input->post('region_id');
			$provinceList = $this->input->post('province_id');
			$cityList = $this->input->post('city_id');
			
			$this->escort_site_settings_on_zones_m->delete_by('site_id', $id);
			foreach($countryList as $key => $value){
				if($countryList[$key]){
					$country_id = $countryList[$key];
				}else{
					$country_id = 0;
				}
				if($regionList[$key]){
					$region_id = $regionList[$key];
				}else{
					$region_id = 0;
				}
				if($provinceList[$key]){
					$province_id = $provinceList[$key];
				}else{
					$province_id = 0;
				}
				if($cityList[$key]){
					$city_id = $cityList[$key];
				}else{
					$city_id = 0;
				}
				
				$countryData = $this->_ci->countries_entity->init('id',$country_id);
				if($countryData){
					$countryName = $countryData->getCountryName();
				}else{
					$countryName = lang('escort_site_setting_zone_all_country_label');
				}
				$stateName = lang('escort_site_setting_zone_all_state_label');
				$provinceName = lang('escort_site_setting_zone_all_province_label');
				$cityName = lang('escort_site_setting_zone_all_city_label');
				if($countryData){
					if($countryData->getCountryCode() == "ES"){
						$stateData =  $this->_ci->states_spain_entity->init('id',$region_id);
						if($stateData){
							$stateName = $stateData->getRegionNameSpanish();
						}
						$provinceData = $this->provinces_spain_entity->init('id',$province_id);
						if($provinceData){
							$provinceName = $provinceData->getProvinceNameSpanish(); 
						}
						$cityData = $this->cities_spain_entity->init('id',$city_id);
						if($cityData){
							$cityName = $cityData->getCityNameSpanish(); 
						}
					}else{
						$stateData =  $this->_ci->states_entity->init('id',$region_id);
						if($stateData){
							$stateName = $stateData->getStateName();
						}
						$cityData = $this->cities_entity->init('id',$city_id);
						if($cityData){
							$cityName = $cityData->getCityName(); 
						}
					}
				}
				
				$this->escort_site_settings_on_zones_m->insert(array(
					'site_id'	=> $id,
					'country_id'	=> $country_id,
					'region_id'	=> $region_id,
					'province_id'	=> $province_id,
					'city_id'	=> $city_id,
					'country_name' => $countryName,
					'state_name'	=> $stateName,
					'province_name'	=> $provinceName,
					'city_name'		=> $cityName
				));
			}
			if ($result)
			{
				$this->session->set_flashdata(array('success' => sprintf(lang('escort_site_setting_edit_success'))));
			}
	
			else
			{
				$this->session->set_flashdata('error', $this->lang->line('escort_site_setting_edit_error'));
			}
	
			// Redirect back to the form or main page
			$this->input->post('btnAction') == 'save_exit' ? redirect('admin/site_setting') : redirect('admin/site_setting/edit/' . $id);
		}
	
		// Go through all the known fields and get the post values
		foreach ($this->validation_rules as $key => $field)
		{
			if (isset($_POST[$field['field']]))
			{
				$post->$field['field'] = set_value($field['field']);
			}
		}
		
		$this->data->languages = array();
		$languageEntity = new CI_Language_entity();
		
		if ($languages = $languageEntity->getLanguageList())
		{
			foreach ($languages as $language)
			{
				$this->data->languages[$language->id] = $language->language;
			}
		}
		$ci=& get_instance();
		$defaultLanguageId = $ci->escort_setting_entity->getDefaultLanguageId();
		
		$this->data->sites = array();
		$siteEntity = new CI_Site_entity();
		if ($sites = $siteEntity->getSiteList())
		{
			foreach ($sites as $site)
			{
				$this->data->sites[$site->id] = $site->name;
			}
		}
		
		$this->data->categories = array();
		
		
		if ($categories = $ci->category_entity->getCategoryListByLanguage($defaultLanguageId))
		{
			foreach ($categories as $category)
			{
				$this->data->categories[$category->code] = $category->category;
			}
		}
		
		
		if ($sexualOrientations = $ci->sexual_orientation_entity->getSexualOrientationListByLanguage($defaultLanguageId))
		{
			foreach ($sexualOrientations as $sexualOrientation)
			{
				$this->data->sexualOrientations[$sexualOrientation->code] = $sexualOrientation->orientation;
			}
		}
		
		$this->data->priorityOptions = array();
		
		if ($priorityOptions = $ci->priority_option_entity->getPriorityOptionList())
		{
			foreach ($priorityOptions as $priorityOption)
			{
				$this->data->priorityOptions[$priorityOption->id] = $priorityOption->priority;
			}
		}
		
		$this->data->hairColors = array();
		if ($hairColors = $ci->hair_color_option_entity->getHairColorOptionListByLanguage($defaultLanguageId))
		{
			foreach ($hairColors as $hairColor)
			{
				$this->data->hairColors[$hairColor->code] = $hairColor->hair_color;
			}
		}
		
		$this->data->hairSizes = array();
		if ($hairSizes = $ci->hair_size_option_entity->getHairSizeOptionListByLanguage($defaultLanguageId))
		{
			foreach ($hairSizes as $hairSize)
			{
				$this->data->hairSizes[$hairSize->code] = $hairSize->hair_style;
			}
		}
		
		$this->data->hairStyles = array();
		if ($hairStyles = $ci->hair_style_option_entity->getHairStyleListByLanguage($defaultLanguageId))
		{
			foreach ($hairStyles as $hairStyle)
			{
				$this->data->hairStyles[$hairStyle->code] = $hairStyle->hair_color;
			}
		}
		
		$this->data->eyeCodes = array();
		if ($eyeCodes = $ci->eye_option_entity->getEyeOptionListByLanguage($defaultLanguageId))
		{
			foreach ($eyeCodes as $eyeCode)
			{
				$this->data->eyeCodes[$eyeCode->code] = $eyeCode->eye;
			}
		}
		
		
		$post->type = 'wysiwyg-advanced';
		$this->template
		->title($this->module_details['name'], sprintf(lang('escort_news_edit_title'), $post->site_id))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
		->append_metadata(js('jquery/jquery.tagsinput.min.js'))
		->append_metadata(js('blog_form.js', 'blog'))
		->append_metadata(css('jquery/jquery.tagsinput.css'))
		->set('post', $post)
		->set('siteSettingZones', $siteSettingZones)
		->build('admin/form');
	}
	public function get_state_data(){
		$country_id = $this->input->post('country_id');
		
		$ci = &get_instance();
		$current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
		
		$COUNTRY = $ci->countries_entity->init('id',$country_id);
		$ci->db->set_dbprefix($current_dbprefix);
		$result = array();
		if($COUNTRY->getCountryCode() == 'ES'){
			$result['spain'] = true;
		}else{
			$result['spain'] = false;
		}
		
		$stateList = getStateData_ioc($country_id);
		$optionList = '<option value="">'.lang("escort_site_setting_zone_select_state_label").'</option>';
		if($stateList){
			foreach ($stateList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.utf8_encode($value).'</option>';
			}
		}
		$result['data'] = $optionList;
		echo json_encode($result);
	}
	public function get_city_data(){
		$country_id = $this->input->post('country_id');
		$state_id = $this->input->post('state_id');		
		
		$ci = &get_instance();
		$current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
		
		$COUNTRY = $ci->countries_entity->init('id',$country_id);
		$ci->db->set_dbprefix($current_dbprefix);
		$result = array();
		if($COUNTRY->getCountryCode() == 'ES'){
			$cityList = getProvinceData_ioc($country_id, $state_id);
			$result['spain'] = true;
			$optionList = '<option value="">'.lang("escort_site_setting_zone_select_province_label").'</option>';
		}else{
			$result['spain'] = false;
			$cityList = getCityData_ioc($country_id, $state_id, 0);
			$optionList = '';
		}

		
		if($cityList){
			foreach ($cityList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList; 
		echo json_encode($result);
	}
	public function get_spain_city_data(){
		$country_id = $this->input->post('country_id');
		$state_id = $this->input->post('state_id');
		$province_id = $this->input->post('province_id');

		$cityList = getCityData_ioc($country_id, $state_id, $province_id);
		$optionList = '';
		if($cityList){
			foreach ($cityList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList;
		echo json_encode($result);
	}
	
	public function add_more_zone(){
		$index = $this->input->post('index');
		$data = '<li class="even multi-item">'
				.'	<ul>'
				.'		<li>'
				.'		<label for="country_id">'.lang('escort_site_setting_zone_label').'</label>'
				.'			<select name="country_id[]" id="country_'.$index.'" class="country_select">'
				.'				<option value="0">'.lang('escort_site_setting_zone_select_country_label').'</option>';
		$countryList = getCountryData_ioc();
		foreach ($countryList as $key => $value){
				$data .= '<option value="'.$key.'">'.$value.'</option>';			
		}
		$data.=  '</select>'
				.'		</li>'
				.'		<li>'
				.'			<label for="region_id">&nbsp;</label>'
				.'			<div>'
				.'				<select name="region_id[]" id="state_country_'.$index.'" class="state_select">'
				.'					<option value="0">'.lang('escort_site_setting_zone_select_state_label').'</option>'
				.'				</select>'
				.'			</div>'
				.'		</li>'
				.'		<li id="province_state_country_'.$index.'_wrapper" style="display: none;">'
				.'			<label for="province_id">&nbsp;</label>'
				.'				<select name="province_id[]" id="province_state_country_'.$index.'" class="provice_select">'
				.'					<option value="0">'.lang('escort_site_setting_zone_select_province_label').'</option>'
				.'				</select>'
				.'		</li>'
				.'		<li>'
				.'			<label for="city_id">&nbsp;</label>'
				.'				<select name="city_id[]" id="city_state_country_'.$index.'" class="city_select">'
				.'					<option value="0">'.lang('escort_site_setting_zone_select_city_label').'</option>'
				.'				</select>'
				.'		</li>'
				.'		<li class="delete-domain">'
				.'			'.anchor('#', lang('global:delete'), array('class'=>'btn red delete', 'onclick' => 'return deleteZone(this)')).''
				.'		</li>'
				.'	</ul>'
				.'</li>';
		echo $data;
	}
	/*
	public function user(){
		echo "here";
		$username = "testuser@gmail.com";
		$password = "123456";
		$salt = "defte";
		$password = sha1($password . $salt);
		
		$this->load->model('users_m');
		$this->users_m->insert(array(
			'email' => $username,
			'password' => $password,
			'salt'		=> $salt,
			'group_id' => 1,
			'created_on' => time(),
			'last_login' => time(),
			'active'	=> 1
			)
		);
		
	}
	*/
}
